package org.corey.share.db.service;

import org.corey.share.db.dao.BaseDao;
import org.corey.share.db.entity.PhotoAlbum;
import org.corey.share.db.manager.DefaultDaoFactory;
import org.corey.share.db.manager.EM;
import org.corey.share.misc.PageData;

public class AlbumServiceImpl implements AlbumService {
	private BaseDao dao = new DefaultDaoFactory().creatBaseDao();

	public boolean deleteAlbum(PhotoAlbum album) {
		if (album == null) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		boolean result = dao.remove(album);
		EM.commitTransaction();
		return result;
	}

	public PageData<PhotoAlbum> getPhotoAlbum(int pageIndex, int pageSize) {
		if (pageIndex < 0 || pageSize < 0) {
			throw new IllegalArgumentException();
		}
		return dao.findAll(pageSize, pageIndex, PhotoAlbum.class,
				"createdDate desc");
	}

	public boolean saveAlbum(PhotoAlbum album) {
		if (album == null) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		boolean result = dao.save(album);
		EM.commitTransaction();
		return result;
	}

	public boolean updateAlbum(PhotoAlbum album) {
		if (album == null) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		boolean result = dao.update(album);
		EM.commitTransaction();
		return result;
	}

	public PhotoAlbum getAlbum(Long albumId) {
		if (albumId == null) {
			throw new IllegalArgumentException();
		}
		return (PhotoAlbum) dao.load(albumId, PhotoAlbum.class);
	}

	public boolean deleteAlbum(Long albumId) {
		if (albumId == null) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		PhotoAlbum album = (PhotoAlbum) dao.load(albumId, PhotoAlbum.class);
		boolean result = false;
		if (album != null) {
			result = dao.remove(album);
		}
		return result;
	}
}
